package cn.uai.activiti8.approval.service.impl;

import cn.uai.activiti8.approval.service.ProductManageProcessService;
import cn.uai.activiti8.cache.GlobalCache;
import cn.uai.activiti8.dto.Result;
import cn.uai.activiti8.enums.RespCodeEnum;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static cn.uai.activiti8.enums.RoleEnum.ROLE_PURCHASER;

/**
 * @desc 商品管理流程
 * @author uai
 * @since 2025/8/11 10:03
 * @version 1.0
 */
@Service
public class ProductManageProcessServiceImpl extends InfrastructureServiceImpl implements ProductManageProcessService {

    private static final String PROCESS_DEFINITION_ID = "product-management-key:1:800003";

    /**
     * 启动流程
     * @param body
     * @return
     */
    @Override
    public ProcessInstance start(String body) {
        JSONObject bodyJson = JSON.parseObject(body);
        Map<String, Object> variables = new HashMap<>();
        Long uid = bodyJson.getLong("uid");
        Long rid = userService.findRidByUid(uid);
        dynamicAssignStartVariables(rid, variables);
        return super.start(PROCESS_DEFINITION_ID, variables);
    }

    @Override
    public String complete(String body) {
        Map<String, Object> variables = new HashMap<>();
        JSONObject bodyJson = JSONObject.parseObject(body);
        String taskId = bodyJson.getString("taskId");
        Integer actionType = bodyJson.getInteger("actionType");
        Long uid = bodyJson.getLong("uid");
        Long rid = userService.findRidByUid(uid);
        variables.put("actionType", actionType);
        boolean isPass = false;
        switch (actionType) {
            case 1 -> isPass = true;
            case 2, 3 -> isPass = false;
        }
        variables.put("isPass", isPass);
        dynamicAssignCompleteVariables(rid, variables);
        try {
            taskService.complete(taskId, variables);
        } catch (
                ActivitiObjectNotFoundException e) {
            return Result.exception(RespCodeEnum.TASK_NOT_FOUND.getCode(), "任务 taskId [" + taskId + "] 已完成，或输入的taskId不存在");
        }
        return taskId;
    }

    @Override
    protected void dynamicAssignStartVariables(Object original, Map<String, Object> variables) {
        // 普通店员 给下一级设置变量
        variables.put("storeAssistants", "690028525338693,704092226347077");
    }

    @Override
    protected void dynamicAssignCompleteVariables(Object original, Map<String, Object> variables) {
        Long rid = (Long) original;
        if (ROLE_PURCHASER.getRid().equals(rid)) {
            // 采购员 给下一级设置变量
            variables.put("storeManager", "681374462742597");
        } else if (GlobalCache.PARALLEL_APPROVAL_ROLE.contains(rid)) {
            // 部门主管（店长）、仓管员、经理助理 给下一级设置变量
            variables.put("regionalManager", "681372686954565");
        }
    }
}
